/*********************/
/** ProcessRFID.java */
/*********************/
package atag.apps.mainApp;

import atag.apps.mainApp.client.Exception_Exception;
import atag.apps.mainApp.client.QueryPortPortPort;
import atag.apps.mainApp.client.QueryPortService;
import atag.runtime.DataItem;
import atag.runtime.DataPool;
import atag.runtime.NodeInfo;
import java.util.Enumeration;
import java.util.Hashtable;

public class ProcessRFID implements Runnable {

	private DataPool m_dataPool;

	private NodeInfo m_myState;
                
        private Hashtable inRoom = new Hashtable();
        
        private String currentRFID;

	/** Creates a new instance of ProcessRFID */
	public ProcessRFID(DataPool dp, NodeInfo myconfig) {
		m_dataPool = dp;
		m_myState = myconfig;
	}

	public void run() {
		/*Checking for each DataItem, to see which one was produced*/
		DataItem t_dataItem;
		if((t_dataItem = m_dataPool.getData(IDConstants.T_PROCESSRFID,
			IDConstants.D_RFID)) != null){
			RFID recvdRFID = (RFID) t_dataItem.core();
			this.handleRFIDProduced(recvdRFID);
		}
	}

   private void handleRFIDProduced(RFID r_RFID) {
        int ID = r_RFID.getID();
        String RFID = r_RFID.getRFID();        
        m_myState.debugPrint("[ProcessRFID]Got a ID " + ID + " and RFID "
                + RFID);
        if (inRoom.containsKey(RFID)) {
            inRoom.remove(RFID);
            actuate();
        } else {
            try {
                currentRFID = RFID;
                QueryPortService service = new QueryPortService();
                QueryPortPortPort port = service.getQueryPortPort();
                // TO DO ...update app for multiple partitons                
                String regionLabel=m_myState.getRegionLabel();
                int regionAttr=(Integer) m_myState.getAttributeByName(regionLabel);
                String partition=regionLabel+" "+regionAttr;
                String userID = port.rfidQuery(RFID, partition);
                int lightSetting = port.getLightQuery(userID);
                m_myState.debugPrint("[ProcessRFID]Got a ID" + ID
                    + " and Light Setting " + lightSetting);
                inRoom.put(currentRFID, new Integer(lightSetting));
                actuate();
            } catch (Exception_Exception ex) {
                ex.printStackTrace();
            }
        }

    }
       private void actuate() {

        int lightSetting;
        if(inRoom.isEmpty())
        {
            lightSetting=Constants.DEFAULT_SETTING;
        }
        else
        {
            // Greater of the values is used
            lightSetting=0;
            Enumeration en=inRoom.elements();
            while(en.hasMoreElements())
            {
                int value=((Integer)en.nextElement()).intValue();
                if(value > lightSetting)
                {
                    lightSetting=value;
                }
            }
        }
        RFIDLightSetting m_RFIDLightSetting = new RFIDLightSetting();
        m_RFIDLightSetting.setID(this.m_myState.getMyId());
        m_RFIDLightSetting.setLightSetting(lightSetting);
        DataItem m_dataitem = new DataItem(IDConstants.D_RFIDLIGHTSETTING,
                IDConstants.T_PROCESSRFID, m_RFIDLightSetting);
        m_dataPool.putData(m_dataitem);
    }

}
